{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.preview.min.css' %}">
    <style>
        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;

        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }

        .title-list ul {
            padding-left: 15px;
            padding-top: 10px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div><i class="fa fa-book" aria-hidden="true"></i> 项目文章</div>
                <div class="function">
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url 'wiki_add' project_id=request.tracer.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                    </a>
                    {% if wiki_object %}
                        <a type="button" class="btn btn-primary btn-xs"
                           href="{% url 'wiki_edit' project_id=request.tracer.project.id wiki_id=wiki_object.id %}">
                            <i class="fa fa-edit" aria-hidden="true"></i> 编辑
                        </a>
                        {# data-toggle="modal" 允许弹出框 data-target="#addModal" 绑定模态框id data-whatever="新建文件夹" 自定义对话框名称 #}
                        <a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#delModal"
                           data-whatever="删除文章">
                            <i class="fa fa-trash" aria-hidden="true"></i> 删除
                        </a>
                    {% endif %}
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">
                        {# ajax插入目录 #}
                    </ul>
                </div>
                <div class="col-sm-9 content">
                    {% if wiki_object %}
                        {# 文章内容展示 #}
                        <div id="previewMarkdown">
                            <textarea>{{ wiki_object.content }}</textarea>
                        </div>
                    {% else %}
                        <div style="text-align: center;margin-top: 50px">
                            <h4> 《{{ request.tracer.project.name }}》项目文章 </h4>
                            <a href="{% url 'wiki_add' project_id=request.tracer.project.id %}">
                                <i class="fa fa-plus-circle" aria-hidden="true"></i>
                                新建文章
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    <!-- 删除对话框 -->
    {% if wiki_object %}
        <div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <!-- 警告框 -->
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h4>删除文章！</h4>
                    <p style="padding-top: 20px"> 确定删除文章：《&nbsp;{{ wiki_object }}&nbsp;》 &nbsp;？</p>
                    <p style="padding-bottom: 20px"> 确定后改文章及所有其子文章都会被删除！</p>
                    <p style="text-align: right">
                        <a type="button" class="btn btn-default btn-sm" aria-label="close" data-dismiss="modal">取
                            消
                        </a>
                        <a href="{% url 'wiki_delete' project_id=request.tracer.project.id wiki_id=wiki_object.id %}"
                           type="button" class="btn btn-danger btn-sm">删 除</a>
                    </p>
                </div>
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/jquery.flowchart.min.js' %}"></script>

    <script>
        var WIKI_DETAIL_URL = "{% url 'wiki' project_id=request.tracer.project.id %}";

        $(function () {
            initCatalog();
            initPreviewMarkdown();
        })


        function initPreviewMarkdown() {
            editormd.markdownToHTML('previewMarkdown', {
                htmlDebode: "style,script,iframe",
            })
        }

        function initCatalog() {
            $.ajax({
                url: "{% url "wiki_catalog" project_id=request.tracer.project.id %}",
                type: "GET",
                dataType: "JSON",
                success: function (res) {
                    console.log(res);
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id;
                            var li = $("<li>").attr('id', 'id_' + item.id).append($("<a>").text(item.title).attr('href', href)).append($('<ul>'));
                            if (!item.parent_id) {
                                {#创建一个li标签，id值为item.id, li标签下创建a标签，写入标签文本。并为li标签创建子ul标签 #}
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children('ul').append(li);
                            }
                        })
                    } else {
                        alert("初始化目录失败")
                    }
                }
            })
        }
    </script>
{% endblock %}
